package cn.rongdao.jrkabao.ui.loan.uicomponent;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRCollection;
import com.mifengkong.frtools.util.FRString;

import cn.rongdao.jrkabao.R;

/**
 * Created by jack on 17/5/16
 */

public class LoanCommitNextButtonUIComponent extends LinearLayout {
    public LoanCommitNextButtonUIComponent(Context context) {
        super(context);
    }

    public LoanCommitNextButtonUIComponent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public LoanCommitNextButtonUIComponent(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private TextView tvNext;
    private TextView tvRest;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        tvNext = (TextView) findViewById(R.id.alc_tv_next);
        tvRest = (TextView) findViewById(R.id.alc_tv_rest);
    }
    public void setNextButtonContent(String rest) {
        setNextButtonContent(rest,"下一步");
    }

    public void setNextButtonContent(String rest,String content) {
        if (FRString.isEmpty(rest)) {
            setRestVisible(false);
        } else if ("-1".equals(rest)) {
            setButtonCommit(content);
            setRestVisible(false);
        } else {
            setRestVisible(true);
            if (rest.equals("0")) {
                setButtonNoRest();
            } else {
                setButtonCommit(content);
                setEnabled(true);
                setRestContent(rest);
            }
        }
    }

    public void setBgAndTextColorForLoan(){
        setBackgroundColor(getResources().getColor(R.color.btnEnableColor));
        tvNext.setTextColor(getResources().getColor(R.color.white));
        tvRest.setTextColor(getResources().getColor(R.color.white));
    }

    public void setButtonCommit(String content) {
        setEnabled(true);
        setNextContent(content);
    }

    public void setButtonNext() {
        setEnabled(true);
        setNextContent("下一步");
    }

    public void setButtonCashAdvance() {
        setEnabled(true);
        setNextContent("去取现");
    }

    public void setButtonNext(String rest) {
        setButtonCashAdvance();
        setRestVisible(false);
    }

    private void setRestContent(String rest) {
        tvRest.setText(FRCollection.appendAsString("今日剩余可申请", rest, "笔", ""));
    }

    private void setNextContent(String rest) {
        tvNext.setText(rest);
    }

    private void setButtonNoRest() {
        setNextContent("明天早点来吧");
        setRestContent("0");
        setEnabled(false);
    }

    private void setRestVisible(boolean isVisible) {
        tvRest.setVisibility(isVisible ? VISIBLE : GONE);
    }
}
